﻿from rrDefine import *
from rrTile import *
from rrRect import *
import rrObject

#*******************************************************************************
#
#	MAP
#
#*******************************************************************************

class Map:
	def __init__(self):
		(self.map,self.rooms)=make_map()
	#place the differents object on the  map	
	def place_objects(self, player, objects):
		#first place the player in the starting room
		start_room=0
		(x,y)=self.rooms[start_room].center()
		player.x=x
		player.y=y
		for r in self.rooms:
			(x,y)=r.center()
			weapon=rrObject.MeleeWeapon(1)
			weapon.x=x
			weapon.y=y
			objects.insert(0,weapon)
			
			armor=rrObject.Armor(1)
			armor.x=x
			armor.y=y
			objects.insert(0,armor)
		
		
#*******************************************************************************
#
#	MAKE MAP
#
#*******************************************************************************		
		
def create_room(map,room):
	for i in range(room.x1,room.x2+1):
		for j in range(room.y1,room.y2+1):
			if j==room.y1 or j==room.y2:
				if map[i][j].type!=CORRIDOR: #must not block room entries
					map[i][j].set_type(H_WALL) #draw wall tiles on border if not corridor
				else:
					map[i][j].set_type(FLOOR) #if corridor overlap a room wall replace it by a door tile
			elif i==room.x1 or i==room.x2:
				if map[i][j].type!=CORRIDOR: #must not block room entries
					map[i][j].set_type(V_WALL) #draw wall tiles on border if not corridor
				else:
					map[i][j].set_type(FLOOR) #if corridor overlap a room wall replace it by door tile
			else:
				map[i][j].set_type(FLOOR) #draw floor tile in the middle
			
			if i==room.x1 and j==room.y1 or i==room.x1 and j==room.y2 or i==room.x2 and j==room.y1 or i==room.x2 and j==room.y2 :
				map[i][j].set_type(X_WALL)
	return map
	
#def create_h_tunnel(x1,x2,y):
def create_h_tunnel(map,new_room,prev_room):
	(x1,y1)=prev_room.center()
	(x2,y2)=new_room.center()
	(x,y)=prev_room.center()
	
	#must be an odd number so no corridor is consecutive with an other one
	if x1%2==0:
		x1+=1
	if x2%2==0:
		x2+=1
	if y%2==0:
		y+=1
	
	for i in range(min(x1,x2),max(x1,x2)+1):
		if map[i][y].type==VOID or map[i][y].type==CORRIDOR: #if the corridor overlaps nether or an already existing corridor draw corridor tile
			map[i][y].set_type(CORRIDOR)
		
		elif map[i][y].type==H_WALL or map[i][y].type==V_WALL:
			map[i][y].set_type(O_DOOR) #else it is in a room so draw room floor instead of either pre existing floor or wall (create a entry point)
		
		elif map[i][y].type==FLOOR:
			map[i][y].set_type(FLOOR)
	return map
	
#def create_v_tunnel(y1,y2,x):
def create_v_tunnel(map,new_room,prev_room):
	(x1,y1)=prev_room.center()
	(x2,y2)=new_room.center()
	(x,y)=new_room.center()
	
	#must be an odd number so no corridor is consecutive with an other one
	if y1%2==0:
		y1+=1
	if y2%2==0:
		y2+=1
	if x%2==0:
		x+=1
		
	for i in range(min(y1,y2),max(y1,y2)+1):
		if map[x][i].type==VOID or map[x][i].type==CORRIDOR: #if the corridor overlaps nether or an already existing corridor draw corridor tile
			map[x][i].set_type(CORRIDOR)
		
		elif map[x][i].type==H_WALL or map[x][i].type==V_WALL:
			map[x][i].set_type(O_DOOR)
		elif map[x][i].type==FLOOR:
			map[x][i].set_type(FLOOR) #else it is in a room so draw room floor instead of either pre existing floor
	return map
	
def make_map():
	map = [[ Tile() for y in range(MAP_H) ] for x in range(MAP_W) ]
	
	num_rooms=0
	rooms=[]
	
	#first create all rooms
	for k in range(MAX_ROOMS):
		#generate the new room height and width. must be an odd number
		h=1
		w=1
		while h%2!=0:
			h=random.randint(ROOM_MIN_SIZE,ROOM_MAX_SIZE)
		while w%2!=0:
			w=random.randint(ROOM_MIN_SIZE,ROOM_MAX_SIZE)
		
		#generate random top left corner (the new room starting point). must be an odd number
		x=1
		y=1
		while x%2!=0:
			x=random.randint(0,MAP_W-w-1)
		while y%2!=0:
			y=random.randint(0,MAP_H-h-1)
		
		new_room=Rect(x,y,w,h)
		
		#check if the room intersects with an other one
		failed=False
		for other_room in rooms:
			if new_room.intersect(other_room):
				failed = True
				break
		
		#if the room doesn't intersect with an other one then create it
		if not failed:
			map=create_room(map,new_room)					
			rooms.append(new_room)
			num_rooms+=1
		
	#finally, connect every room with corridors.
	for i in range(num_rooms):
		if i>0:
			#choose randomly to start either with a vertical or horizontal corridor
			if random.randint(0,1):
				map=create_h_tunnel(map,rooms[i],rooms[i-1])
				map=create_v_tunnel(map,rooms[i],rooms[i-1])
			else:
				map=create_v_tunnel(map,rooms[i],rooms[i-1])
				map=create_h_tunnel(map,rooms[i],rooms[i-1])
	
	#change door type
	for i in range(MAP_H):
		for j in range(MAP_W):
			if map[j][i].type==O_DOOR:
				type=random.randint(1,100)
				#if type>95:
				#	map[j][i].set_type(L_DOOR)
				if type>80:
					map[j][i].set_type(U_DOOR)

	return map,rooms
			


#*******************************************************************************
#
#	DRAW MAP
#
#*******************************************************************************
					
def draw_map(map):	
	init_pair(8,COLOR_WHITE,COLOR_BLACK) #seen
	init_pair(9,COLOR_BLACK,COLOR_BLACK) #unseen
	init_pair(10,COLOR_RED,COLOR_BLACK) #lava
	init_pair(11,COLOR_BLUE,COLOR_BLACK) #water
	init_pair(12,COLOR_YELLOW,COLOR_BLACK) #doors
	
	pair=None
	c=None
	bold=False
	
	for i in range(MAP_H):
		for j in range(MAP_W):
			if map[j][i].explored:
				if map[j][i].seen:
					pair=8
					bold=True
					if map[j][i].type==LAVA:
						pair=10
						bold=False
					elif map[j][i].type==WATER:
						pair=11
						bold=False
					elif map[j][i].type==U_DOOR or map[j][i].type==L_DOOR or map[j][i].type==O_DOOR:
						pair=12
						bold=False
				else:
					pair=9
					bold=True
				
				if bold:
					mvaddstr(i+1,j,map[j][i].sprite,COLOR_PAIR(pair)|A_BOLD)
				else:
					mvaddstr(i+1,j,map[j][i].sprite,COLOR_PAIR(pair))
			
			else:
				mvaddstr(i+1,j,' ')	